package com.glory.dgcpcpda.adapter;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.entity.ChangeShiftEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ToastUtils;

import java.util.List;

/**
 * FileName: ChangeShiftsAdapter
 * Author: XJH
 * Date: 2020/7/1 19:31
 * Description:
 * History:
 */
public class ChangeShiftsAdapter extends BaseQuickAdapter<ChangeShiftEntity, BaseViewHolder> {

    public ChangeShiftsAdapter(@Nullable List<ChangeShiftEntity> data) {
        super(R.layout.item_change_shift_adapter,data);
    }

    @Override
    protected void convert(BaseViewHolder helper, ChangeShiftEntity item) {
        helper.setText(R.id.tvLotNum,item.getMAINQTY());
        helper.setText(R.id.tvEqpID,item.getRESERVED03());
        helper.setText(R.id.tvLot,item.getITEMID());
        helper.setText(R.id.tvEqpState,item.getRESERVED04());

        helper.getView(R.id.etNum).setEnabled(false);
        setEditListener(helper.getView(R.id.etFinNum),helper.getView(R.id.etNum),item,true);
        //setEditListener(helper.getView(R.id.etNum),helper.getView(R.id.etNum),item,false);


        ImageView ivDelete = helper.getView(R.id.ivDelete);
        ivDelete.setOnClickListener(view -> {
            remove(helper.getAdapterPosition());
            notifyDataSetChanged();
        });
    }

    private void setEditListener(EditText editText,EditText editText2, ChangeShiftEntity item,boolean type) {
        if (editText.getTag() instanceof TextWatcher) {
            editText.removeTextChangedListener((TextWatcher) editText.getTag());
        }

        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                if (type){
                    if (!s.toString().equals("")){
                        try {
                            if (Integer.parseInt(s.toString()) > Integer.parseInt(item.getMAINQTY())){
                                editText.setText("");
                                editText2.setText("");
                                return;
                            }
                            item.setCOMMENTS(s.toString());

                            editText2.setText(Integer.valueOf(item.getMAINQTY())-Integer.valueOf(Integer.valueOf(s.toString()))+"");
                            item.setRESERVED01(Integer.valueOf(item.getMAINQTY())-Integer.valueOf(Integer.valueOf(s.toString()))+"");
                        }catch (NumberFormatException e){
                            FreeApi_ToastUtils.showFreeToast("请输入数字",mContext,false,0);
                            e.printStackTrace();
                        }
                    }
                }else {
                    item.setRESERVED01(s.toString());
                }
            }
        };

        editText.addTextChangedListener(watcher);
        editText.setTag(watcher);
    }
}
